package plus.easydo.rocketmq.controller;

import org.apache.rocketmq.client.exception.MQBrokerException;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.remoting.exception.RemotingException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import plus.easydo.core.base.controller.BaseController;
import plus.easydo.core.result.R;
import org.apache.rocketmq.common.message.Message;

/**
 * @author laoyu
 * @version 1.0
 * @date 2021/11/3
 */
@RequestMapping("send/")
@RestController
public class SendController extends BaseController {

    public static final Logger LOGGER = LoggerFactory.getLogger(SendController.class);

    @Autowired
    DefaultMQProducer defaultMQProducer;

    @GetMapping("/{msg}")
    public R<Object> send(@PathVariable("msg")String msg) throws MQBrokerException, RemotingException, InterruptedException, MQClientException {

        if (StringUtils.isEmpty(msg)) {
            return fail("消息不能为空");
        }
        LOGGER.info("发送MQ消息内容：{}" , msg);
        Message sendMsg = new Message("TestTopic", "TestTag", msg.getBytes());
        // 默认3秒超时
        SendResult sendResult = defaultMQProducer.send(sendMsg);
        LOGGER.info("消息发送响应：" + sendResult.toString());
        return ok(sendResult);
    }

}
